<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item
              label="车牌号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="vehicleNumber"
            >
              <a-input v-model="model.vehicleNumber" placeholder="请输入车牌号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="车辆品牌名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="vehicleName"
            >
              <a-input v-model="model.vehicleName" placeholder="请输入车辆品牌名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="车辆状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="vehicleState"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.vehicleState"
                dictCode="vehicle_state"
                placeholder="请选择车辆状态"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="车辆总里程（km）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="vehicleTotal"
            >
              <a-input-number
                v-model="model.vehicleTotal"
                placeholder="请输入车辆总里程（km）"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="上次保养时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="maintenanceTime"
            >
              <j-date placeholder="请选择上次保养时间" v-model="model.maintenanceTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="保养周期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="maintenanceCycle"
            >
              <a-input-number
                v-model="model.maintenanceCycle"
                placeholder="请输入保养周期"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="保养里程"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="maintenanceMileage"
            >
              <a-input v-model="model.maintenanceMileage" placeholder="请输入保养里程"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="保养描述"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="maintenanceOfDescription"
            >
              <a-input v-model="model.maintenanceOfDescription" placeholder="请输入保养描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="维修描述"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="describeTheMaintenance"
            >
              <a-input v-model="model.describeTheMaintenance" placeholder="请输入维修描述"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WorkVehiclesForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        vehicleNumber: [{ required: true, message: '请输入车牌号!' }],
        vehicleName: [{ required: true, message: '请输入车辆品牌名称!' }],
        vehicleState: [{ required: true, message: '请输入车辆状态0，未借用，1，以借用，2维修中!' }],
        vehicleTotal: [{ required: true, message: '请输入车辆总里程（km）!' }]
      },
      url: {
        add: '/vehicle/workVehicles/add',
        edit: '/vehicle/workVehicles/edit',
        queryById: '/vehicle/workVehicles/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>